/**
 * 
 */
package edu.cornell.cs.lsi.projone.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import edu.cornell.cs.lsi.projone.common.LSIException;

/**
 * @author poornima
 * 
 */
public class GroupMembershipService extends Thread {

	private final static Log LOGGER = LogFactoryImpl
			.getLog(GroupMembershipService.class);

	private final long probeTime;
	private final long threadWaitTime;
	private GroupMemberManager grpMgr = null;
	private static GroupMembershipService grpService = new GroupMembershipService();
	private static boolean isStarted = false;

	public GroupMembershipService() {
		grpMgr = new GroupMemberManager();
		probeTime = 1000;
		threadWaitTime = 5000;
	}

	public void startThread() {
		if (!isStarted) {
			grpService.start();
			isStarted = true;
		}
	}

	@Override
	public void run() {
		LOGGER.info("Grp Member service run starts.....");
		while (true) {
			try {
				grpMgr.scanGroup();
				sleep(threadWaitTime);
			} catch (LSIException e) {
				LOGGER.error("GroupMembershipService: Error while scanning the group...");
				e.printStackTrace();
			} catch (InterruptedException e) {
				LOGGER.warn("GroupMembershipService: The Group membership service thread is notified...");
				// e.printStackTrace();
			}
		}
	}
}
